#!/usr/bin/ruby -d

require 'net/smtp'
require 'reminder'
require 'taskmgr' # due to TaskMgr module - move it to separate file


class TaskScheduler < TaskMgr

  def initialize()
    super
  end

  def send_all_reminders
    current_tasks = Array.new
    now = Time.new
    current_date = now.to_i
    # This raise seems to not working.
    raise "No tasks for today" if !(current_tasks = $global_storage.get_pending_tasks(current_date))
    current_tasks.each do |task|
      id        = task[0]
      submitter = task[1]
      subject   = task[2]
      date      = task[3]
      pending   = task[4]
      rem = Reminder.new(submitter, subject)
      error_code = rem.send
      if (error_code =~ /^2/)
        $global_storage.reset_pending(date)
      end
    end
  end

end
